package com.zyc.app.woa.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.*;
import com.umeng.fb.FeedbackAgent;
import com.umeng.fb.model.Conversation;
import com.umeng.fb.model.DevReply;
import com.umeng.fb.model.Reply;
import com.umeng.fb.model.UserInfo;
import com.zyc.app.woa.R;
import com.zyc.app.woa.utils.CommonUtils;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: yingzhang.zhang
 * Date  : 2014/9/1.
 */
public class ConversationActivity extends BaseActivity implements OnClickListener {
    private ListView mListView;
    private EditText mEditText;
    private Button mSendButton;
    private Conversation mConversation;
    private FeedbackAgent mFeedbackAgent;
    private ReplyListAdapter mReplyListAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversation_layout);
        mListView = (ListView) findViewById(R.id.umeng_fb_reply_list);
        mEditText = (EditText) findViewById(R.id.umeng_fb_reply_content);
        mSendButton = (Button) findViewById(R.id.umeng_fb_send);

        mFeedbackAgent = new FeedbackAgent(this);
        mConversation = mFeedbackAgent.getDefaultConversation();
        mSendButton.setOnClickListener(this);
        mReplyListAdapter = new ReplyListAdapter(this);
        mListView.setAdapter(mReplyListAdapter);
        getActionBar().setTitle(getString(R.string.feedback));
        syncReply();
    }

    @Override
    public void onClick(View view) {
        if (view == mSendButton) {
            onSendButtonClick();
        }
    }

    private void onSendButtonClick() {
        addUserInfo();
        String content = mEditText.getText().toString().trim();
        if (!TextUtils.isEmpty(content)) {
            mEditText.getEditableText().clear();
            mConversation.addUserReply(content);
            syncReply();
            CommonUtils.hideSoftInputWindow(mEditText.getWindowToken());
        }
    }

    private void syncReply() {
        mConversation.sync(new Conversation.SyncListener() {
            @Override
            public void onReceiveDevReply(List<DevReply> devReplies) {
                mReplyListAdapter.notifyDataSetChanged();
            }

            @Override
            public void onSendUserReply(List<Reply> replies) {

            }
        });
    }

    private void addUserInfo() {
        try {
            UserInfo userInfo = mFeedbackAgent.getUserInfo();
            if (null == userInfo) {
                userInfo = new UserInfo();
            }

            Map<String, String> contact = userInfo.getContact();
            if (null == contact) {
                contact = new HashMap<String, String>();
            }

            contact.put("appVersion: ", CommonUtils.getAppVersion());
            userInfo.setContact(contact);
            mFeedbackAgent.setUserInfo(userInfo);
        }catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public class ReplyListAdapter extends BaseAdapter {
        private boolean mShowUserMsgDate = false;
        private boolean mShowDevMsgDate = false;
        Context mContext;
        LayoutInflater mInflater;

        public ReplyListAdapter(Context context) {
            mContext = context;
            mInflater = LayoutInflater.from(mContext);
        }

        @Override
        public int getCount() {
            List<Reply> replyList = mConversation.getReplyList();
            return (replyList == null) ? 0 : replyList.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.conversation_list_item, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            Reply reply = mConversation.getReplyList().get(position);
            if (reply instanceof DevReply) {
                bindDevReplyData(holder, reply);
            } else {
                bindUserReplyData(holder, reply);
            }
            return convertView;
        }

        @Override
        public Object getItem(int position) {
            return mConversation.getReplyList().get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        private void bindDevReplyData(ViewHolder holder, Reply reply) {
            if (mShowDevMsgDate) {
                String time = SimpleDateFormat.getDateTimeInstance().format(reply.getDatetime());
                holder.mDate.setText(time);
                mShowDevMsgDate = false;
            }

            holder.mLeftMessage.setVisibility(View.VISIBLE);
            holder.mRightMessage.setVisibility(View.INVISIBLE);
            holder.mLeftMessageContent.setText(reply.getContent());
        }

        private void bindUserReplyData(ViewHolder holder, Reply reply) {
            if (mShowUserMsgDate) {
                String time = SimpleDateFormat.getDateTimeInstance().format(reply.getDatetime());
                holder.mDate.setText(time);
                mShowUserMsgDate = false;
            }

            holder.mRightMessage.setVisibility(View.VISIBLE);
            holder.mLeftMessage.setVisibility(View.INVISIBLE);
            holder.mRightMessageContent.setText(reply.getContent());
        }

        class ViewHolder {
            TextView mDate;
            RelativeLayout mLeftMessage;
            RelativeLayout mRightMessage;
            ImageView mLeftAvatar;
            ImageView mRightAvatar;
            TextView mLeftMessageContent;
            TextView mRightMessageContent;

            public ViewHolder(View view) {
                mDate = (TextView) view.findViewById(R.id.umeng_fb_reply_date);
                mLeftMessage = (RelativeLayout) view.findViewById(R.id.left_umeng_fb_message);
                mRightMessage = (RelativeLayout) view.findViewById(R.id.right_umeng_fb_message);
                mLeftAvatar = (ImageView) view.findViewById(R.id.left_umeng_fb_reply_avatar);
                mRightAvatar = (ImageView) view.findViewById(R.id.right_umeng_fb_reply_avatar);
                mLeftMessageContent = (TextView) view.findViewById(R.id.left_umeng_fb_reply_content);
                mRightMessageContent = (TextView) view.findViewById(R.id.right_umeng_fb_reply_content);
            }
        }
    }

}
